$().ready(function() {
	loadProject();
	validateRule();
	laydate({
		elem : '#outTime'
	});
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
function save() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/pro/outstock/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			sku : {
				required : true
			},
			groupNumber : {
				required : true
			},
			outQty : {
				required : true
			},
			purPrice : {
				required : true
			}
		},
		messages : {
			sku : {
				required : icon + "请输入SKU"
			},
			groupNumber : {
				required : icon + "请选择采购合同编号"
			},
			groupNumber : {
				required : icon + "请输入出库数量"
			},
			groupNumber : {
				required : icon + "请输入采购单价"
			}
		}
	})
}
function getSku() {
	var sku = $("#sku").val();
	if(sku != null && sku.length>0){
		$.ajax({
			url:"/pro/product/getsku",
			data: {
				sku: function () {
					return $("#sku").val();
				}
			},
			type:"Post",
			dataType:"json",
			success:function(data){
				$("#skuid").val(data.id);
				$("#skuName").val(data.name);
				$("#skuModel").val(data.proModel);
				$("#skuSpec").val(data.proSpec);
				$("#groupNumber").val(null);
				$("#inId").val(null);
				$("#inQty").val(null);
				$("#newoutQty").val(null);
				$("#purPrice").val(null);
				$("#supplierId").val(null);
				$("#supplierName").val(null);
				$("#inTime").val(null);
				$("#warehouseId").val(null);
				$("#warehouseName").val(null);
				loadPurInfo(data.id);
			},
			error:function(data){
				layer.msg("SKU错误");
			}
		});
	}else{
		layer.msg("请输入SKU");
	}
}

function loadPurInfo(proid){
	var html = "";
	$.ajax({
		url : '/pro/instock/getgrouplist/' + proid,
		success : function(data) {
			/*$(".chosen-select").empty();*/
			$(".chosen-select").val("");
			html ='<option value="">采购合同编号</option>';
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].id + '">' + data[i].groupid + '</option>'
			}
			$(".chosen-select").html(html);
			$(".chosen-select").trigger("chosen:updated");
			$(".chosen-select").chosen({
				maxHeight : 200
			});
			//点击事件
			$('.chosen-select').on('change', function(e, params) {
				console.log(params.selected);
				var opt = {
					query : {
						type : params.selected,
					}
				}
				$('#exampleTable').bootstrapTable('refresh', opt);
				$.ajax({
					url :"/pro/instock/getInfo/"+params.selected,
					data : null,
					type:"get",
					success : function(data) {
						$("input[name='inId']").val(data.id);
						$("input[name='groupNumber']").val(data.groupid);
						$("input[name='inQty']").val(Number(data.inqty));
						$("input[name='newoutQty']").val(Number(data.inqty)-Number(data.outQty));
						$("input[name='purPrice']").val(Number(data.purPrice));
						$("input[name='supplierId']").val(data.supplierId);
						$("input[name='supplierName']").val(data.supplierName);
						$("input[name='inTime']").val(data.inqtyTime);
						$("input[name='warehouseId']").val(data.warehouseId);
						$("input[name='warehouseName']").val(data.warehouseName);
					}
				});
			});

		}
	});
}

//查询项目
function loadProject() {
	var html = "";
	$.ajax({
		url : '/itm/project/getProjectlist',
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].id + '">' + data[i].itmNumber + '</option>'
			}
			$(".chosen-select-peojiectId").append(html);
			$(".chosen-select-peojiectId").chosen({
				maxHeight : 200
			});
			//点击事件
			$('.chosen-select-peojiectId').on('change', function(e, params) {
				console.log(params.selected);
				var opt = {
					query : {
						type : params.selected,
					}
				}
				$('#exampleTable').bootstrapTable('refresh', opt);
			});
		}
	});
}

//上传《送货单》
layui.use('upload', function(){
	var upload = layui.upload;
	//执行实例
	var uploadInst = upload.render({
		elem: '#upLoad' //绑定元素
		,url: '/common/sysFile/upload' //上传接口
		,before: function(obj){
			//预读本地文件示例，不支持ie8
			obj.preview(function(index, file, result){
				$('#pic').attr('src', result); //图片链接（base64）
			});
		}
		,done: function(res){
			//上传完毕回调
			console.log(res,"fileName");
			$('#outPic').val(res.fileName)
		}
		,error: function(){
			//请求异常回调
		}
	});
})